package com.renbang.action.finance;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.renbang.model.common.AjaxResult;
import com.renbang.model.common.Page;
import com.renbang.model.finance.FinanceOrg;
import com.renbang.service.finance.FinanceOrgService;
import com.renbang.utils.excel.ExcelImport;
import com.renbang.utils.mail.MailBean;
import com.renbang.utils.mail.MailUtil;
import com.renbang.action.BaseAction;
import com.renbang.dao.sqlsession.DBUtils;

/**
 * 该代码由程序生成
 */
@Controller
@RequestMapping(value = "user/finance/FinanceOrg")
public class FinanceOrgAction extends BaseAction<FinanceOrg> {
	@Autowired
	FinanceOrgService financeOrgService;

	
	//重写父类方法。方便调试
	@RequestMapping(value = "/showList")
	public  @ResponseBody AjaxResult showList(HttpServletRequest request,Page page,FinanceOrg obj) {
		System.out.println(obj);
		System.out.println(page);
		Integer count = financeOrgService.findTotal(obj);
		return new AjaxResult(AjaxResult.STATUS_OK, page.getCurrpage()+"","数据加载成功" ,financeOrgService.findInPage(obj,page), count);
	}
	
	// 展示当前用户申报
	@RequestMapping(value = "/showApplyList")
	public @ResponseBody AjaxResult showApplyList(HttpServletRequest request,
			Page page, FinanceOrg obj) {
		obj.setUserId(getUser().getUserId());
		Integer count = financeOrgService.findTotal(obj);
		return new AjaxResult(AjaxResult.STATUS_OK, page.getCurrpage() + "",
				"数据加载成功", financeOrgService.findInPage(obj, page), count);
	}

	// 打开申报页
	@RequestMapping(value = "/editForm")
	public String editForm(Integer id, HttpServletRequest request) {
		FinanceOrg financeOrg;
		if (id != null) {
			financeOrg = financeOrgService.findById(id);
			request.setAttribute("obj", financeOrg);
		}
		return "user/finance/FinanceOrg-applyForm";
	}

	// 打开审查页
	@RequestMapping(value = "/checkForm")
	public String checkForm(Integer id, HttpServletRequest request) {
		FinanceOrg financeOrg;
		if (id != null) {
			financeOrg = financeOrgService.findById(id);
			request.setAttribute("obj", financeOrg);
		}
		return "user/finance/FinanceOrg-checkForm";
	}

	/**
	 * 
	 * @Description:审核
	 * @author:user
	 * @param financeOrg
	 * @return AjaxResult
	 * @date 2017年5月11日
	 */
	@RequestMapping(value = "/check")
	public @ResponseBody AjaxResult Check(HttpServletRequest request,
			FinanceOrg financeOrg) {
		int i = financeOrgService.modify(financeOrg);
		if (financeOrg.getOrgEmail() != null&&!financeOrg.getOrgEmail().equals("")) {  
			MailBean mailBean = new MailBean();
			mailBean.setSubject("审核通知");
			mailBean.setReceiver(financeOrg.getOrgEmail());
			String msg = "";
			if (financeOrg.getOrgStatus() == 1)
				msg = "您的申报已通过审核！";
			 else 
				msg = "您的申报未通过审核！";
			mailBean.setMessage(msg);
			//发送邮件
			new MailUtil().sendHtmlEmail(mailBean);
		}
		if (i > 0)
			return new AjaxResult(AjaxResult.STATUS_OK, "审核成功");
		else
			return new AjaxResult(AjaxResult.STATUS_OK, "审核失败");
	}

	
	@RequestMapping(value = "/uploadImg")
	public @ResponseBody AjaxResult importChance(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile file)
			throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		String fileName = file.getOriginalFilename();
		String dirPath = "D:/webresources/";
		String saveDir = "image";
		dirPath+=saveDir;
		String saveUrl = "http://192.168.1.13:1088/";
		saveUrl+=saveDir;
		File fileDir = new File(dirPath);
		if (!fileDir.exists()||!fileDir.isDirectory()) {
			if (fileDir.mkdirs()) {
			}
		}
		File saveFile = new File(dirPath + "/" + fileName);
		saveUrl += "/"+fileName;
		try {
			file.transferTo(saveFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK, saveUrl);
	}
	
	
	
	
	/**
	 * @Description:新增或修改
	 * @author:user
	 * @param FinanceOrgAction
	 *            .java
	 * @return AjaxResult
	 * @date 2017年5月10日
	 */
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult insertOrUpdata(HttpServletRequest request,
			FinanceOrg financeOrg) {
		if (financeOrg.getId() != null) {
			//修改也要重新审核
			financeOrg.setOrgStatus(-1);
			int i = financeOrgService.modify(financeOrg);
			if (i > 0)
				return new AjaxResult(AjaxResult.STATUS_OK, "修改成功");
			else
				return new AjaxResult(AjaxResult.STATUS_OK, "修改失败");
		} else {
			financeOrg.setOrgStatus(-1);
			financeOrg.setUserId(getUser().getUserId());
			int i = financeOrgService.add(financeOrg);
			if (i > 0)
				return new AjaxResult(AjaxResult.STATUS_OK, "增加成功");
			else
				return new AjaxResult(AjaxResult.STATUS_OK, "增加失败");
		}

	}

}
